package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建map集合
        Map<String,Object> result = new HashMap<>();
        //将专辑添加到布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        bloomFilter.add(albumId);
        // 多线程查询数据
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
            //远程调用
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "查询数据为空");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo,"查询专辑信息失败");
            result.put("albumInfo",albumInfo);
            //返回数据
            return albumInfo;
        },threadPoolExecutor).whenCompleteAsync((v,t)->{
            System.out.println("专辑标题 111= " + v.getAlbumIntro());
            if(t != null){
                log.error("查询专辑信息失败:{}",t.getMessage());
            }
        });
        //获取分类数据
        CompletableFuture<Void> cateCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getBaseCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "查询分类信息失败");
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            Assert.notNull(baseCategoryView, "查询分类信息失败");
            // 存储数据
            result.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询分类信息失败:{}", t.getMessage());
            }
        });
        //获取用户id
        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "查询作者信息失败");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "查询作者信息失败");
            result.put("announcer", userInfoVo);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询作者信息失败:{}", t.getMessage());
            }
        });
        //  根据专辑Id 查询专辑统计数据;
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            //  远程调用;
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "查询专辑统计数据失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "查询专辑统计数据失败");
            result.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询统计数据失败：{}", t.getMessage());
            }
        });
        //多任务组合
        try {
            CompletableFuture.allOf(albumInfoCompletableFuture,cateCompletableFuture,userCompletableFuture,statCompletableFuture).join();
        } catch (Exception e) {
            throw new GuiguException(8888,"查询专辑数据失败");
        }
        //返回数据
        System.out.println(result);
        return result;
    }
}
